package com.xms.module.service;

import com.xms.core.module.ModuleCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * <p>
 * ModuleLoader
 * </p>
 *
 * @author migo
 * @since 2021/10/24
 */
@Component
public class ModuleLoader implements ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ModuleLoader.class);
    private IModuleService _moduleService;

    @Autowired
    public ModuleLoader(IModuleService moduleService) {
        _moduleService = moduleService;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        LOGGER.info("加载模块信息");
        try {
            ModuleCollection.setModules(_moduleService.list());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("加载模块信息失败");
        }
        LOGGER.info("完成加载模块信息");
    }
}
